package main

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"strconv"
	"strings"
)

func main() {
	questionFile := "D:/materiel/question.txt"
	questionList := GetQuestionList(questionFile)
	for _, question := range questionList {
		answer := GetAnswer(fmt.Sprintf("D:/materiel/answer/%s.txt", question.Id))
		fmt.Printf("id=%s, desc=%s\n", question.Id, question.Desc)
		fmt.Println(answer)
		break
	}

}

func GetQuestionList(file string) []Question {
	bytes, err := ioutil.ReadFile(file)
	if err != nil {
		fmt.Println("read file error.", err.Error())
		return nil
	}
	contents := string(bytes)
	lines := strings.Split(contents, "\n")
	questions := make([]Question, 0)
	for _, line := range lines {
		split := strings.Split(line, "\t") // split[0]=id, split[1]=desc
		questions = append(questions, Question{split[0], split[1]})
	}
	return questions
}

func GetAnswer(file string) string {
	bytes, err := ioutil.ReadFile(file)
	if err != nil {
		fmt.Println("read file error.", err.Error())
		return ""
	}
	content := string(bytes)
	ascii := strconv.QuoteToASCII(content) //包含双引号
	answer := Answer{0, ascii[1 : len(ascii)-1]}
	marshal, _ := json.Marshal(answer)
	return string(marshal)
}

type Question struct {
	Id string	//问题ID
	Desc string	//问题描述
}

type Answer struct {
	MsgType int8 `json:"msgType"`
	MsgContent string `json:"msgContent"`
}